<?php

class Loader
{
	
	private $DF;
	
	function __construct ( $DF )
	{
		$this->DF = $DF;
	} 

	/*------------------------------------------------
	 Do the actual loading
		$_0: folder path
		$_1: class name
		$_2: desired object name
		$_3: additional arguments for the controller's
				 constructor
	--------------------------------------------------*/
	private function loadClass ( $_0, $_1, $_2 = '', $_3 = '' )
	{
		# Build full absolute path to class file
		$file  = $_0 . '.php';

		# Derive the class name from the file name
		$class =  $_1;
	
		# Check to make sure the file actually exists on the server
		if ( !file_exists ( $file ) )
		{
			#>>>>DEBUGGING
			if ( _DEBUG_ )
			{
				echo "__xCould not load: ", $file, "<br>";
			}
			
			return FALSE;
		}
		
		# If we made it this far, the file exists. Let's require it.
		require_once ( $file );
		
		# Make sure the class exists at this point.
		if ( !class_exists ( $class ) )
		{			
			#>>>>DEBUGGING
			if ( _DEBUG_ )
			{
				echo "__xCould not load: ", $file, " [", $class, "]<br>";
			}
			
			return FALSE;
		}
		
		# Instantiate the class. The class MUST ACCEPT the DF object in its constructor!
		# Check to see if an additional argument is set for the class
		$newClass =  new $class ( $this->DF, $_3 );

		
		#>>>>DEBUGGING
		if ( _DEBUG_ )
		{
			echo "__xSuccessfully loaded: ", $file, " [", $class, "]<br>";
		}
		
		# Add the new object to the DF object for global application use
		return $this->DF->set ( $_2, $newClass );
	}
	
	/*------------------------------------------------
	 load a model
		$_0: model name
		$_1: desired object name
		$_2: arguments for the model's constructor
	--------------------------------------------------*/
	function model ( $_0, $_1 = '', $_2 = '' )
	{
		$path = DIR_APP_MODELS . $_0 . '.model';
		
		$modelName = 'Model_' . $_0;
		
		return $this->loadClass ( $path, $modelName, $_1, $_2 );
	}
	
	/*------------------------------------------------
	 load a view
		$_0: view name
		$_1: desired object name
		arguments for the view's constructor
	--------------------------------------------------*/
	function view ( $_0, $_1 = '', $_2 = '' )
	{
		$path = DIR_APP_VIEW;
		
		return $this->loadClass ( $path, $_0, $_1, $_2 );
	}
	
	/*------------------------------------------------
	 load a controller
		$_0: controller name
		$_1: desired object name
		$_2: arguments for the controller's constructor
	--------------------------------------------------*/
	function controller ( $_0, $_1 = '', $_2 = '' )
	{
		$path = DIR_APP_CONTROLLERS;
		
		return $this->loadClass ( $path, $_0, $_1, $_2 );
	}
	
	/*------------------------------------------------
	 load a library
		$_0: library name
		$_1: desired object name<br />
		$_2: arguments for the library's constructor
	--------------------------------------------------*/
	function library ( $_0, $_1 = '', $_2 = '')
	{
		if ( substr ( $_0, 0, 3 ) == 'DF_' )
		{
			$path = DIR_DF_LIB . strtolower ( substr ( $_0, 3 ) );
		}
		else
		{
			$path = DIR_APP_LIB . $_0;
		}
		
		return $this->loadClass ( $path, $_0, $_1, $_2 );
		
	}
	
	/*------------------------------------------------
	 load a core class
		$_0: class name
		$_1: desired object name
		$_2: arguments for the core library's constructor
	--------------------------------------------------*/
	function core ( $_0, $_1 = '', $_2 = '' )
	{
		$path = DIR_DF_CORE . strtolower ( $_0 );
		
		return $this->loadClass ( $path, $_0, $_1, $_2 );
	}
}
?>